{% extends './layout.tpl' %}
{% block css_assets %}
{% endblock %}

{% block content %}
<div class="layadmin-user-login-main">
	<div class="layadmin-user-login-box layadmin-user-login-header">
		<h2>登录<i style="font-style: normal;color:#999;font-size:10px;margin-left: 5px;">{{ version }}</i></h2>
		<p></p>
	</div>
	<div class="layadmin-user-login-box layadmin-user-login-body layui-form">
		<form class="layui-form" action="">
			<div class="layui-form-item">
				<label class="layadmin-user-login-icon iconfont icon-profile" for="user-login-username"></label>
				<input type="text" name="username"  lay-verify="username" placeholder="邮箱或mt账号" value="18516035775" class="layui-input" >
			</div>
			<div class="layui-form-item">
				<label class="layadmin-user-login-icon iconfont icon-unlock" for="user-login-password"></label>
				<input type="password" name="password" lay-verify="password" placeholder="密码" value="Mdf123456" class="layui-input" >
			</div>
			<div class="layui-form-item" id="captcha-item">
				<input type="text" name="captcha" lay-verify="captcha" placeholder="验证码" class="layui-input" >
				<img src="/common/captcha/get" onclick="this.src='/common/captcha/get?r='+Math.random();"/>
			</div>
			<div class="layui-form-item">
				<a href="/sale/password/retrieve">忘记密码</a>
			</div>
			<div class="layui-form-item">
				<button class="layui-btn layui-btn-fluid" lay-submit=""  lay-filter="user-login-submit" >登 入</button>
			</div>
		</form>
	</div>
</div>
{% endblock %}



{% block js_assets %}

	<script>

        layui.use(['form', 'layedit', 'laydate'], function(){
            var form = layui.form
                ,layer = layui.layer

            form.verify({
                username: function(value){
                    if(value.length == 0){
                        return '请输入登录账号';
                    }
                    if(!emailReg.test(value) && !mtReg.test(value)){
                        return '请输入正确的账号';
                    }
                },
                captcha:function(value){
                    if(errorNum > captchaNum){
                        if( value.trim().length == 0){
                            return '请输入验证码'
                        }
                    }
                },
                password: function(value){

                    if(value.length == 0){
                        return '请输入密码';
                    }
                }
                /* ,password: [passwordReg, '请输入正确的密码']*/
            });

            //监听提交
            form.on('submit(user-login-submit)', function(data){
                if(errorNum > captchaNum){
                    checkCaptcha(data.field)
                }else{
                    signup(data.field)
                }
                return false;
            });


        });

        function signup(data){
            $.post("/sale/signup",data,function(result){
                if(result.status !=200){
                    setCaptcha()
                    layer.msg(result.msg,{icon:2})
                    return false
                }

                var datas = JSON.parse(result.datas)
                var user = {
                    role_type:datas.role_type,
                    user_id:datas.user_id,
                    email:datas.user.email
                }
                addCookie("saletoken", datas.token, 0,"/sale")
                addCookie("user", JSON.stringify(user), 0,"/sale")
                layer.msg("登录成功",{icon:1, time: 1000,},function () {
                    window.location.href = "/sale/userinfo"
                },100)


            });
        }
	</script>
{% endblock %}

